本章節將使用套件ggplot2,同前一節,繪出散佈圖、線圖、直方圖、盒鬚圖和長條圖。
ggplot2# 安裝套件
install.packages("ggplot2")
# 載入套件
library(ggplot2)載入套件後,將發現將新增更多玩具資料,可於data()查詢。
接下來將一致使用ggplot函數繪圖。
玩具資料採用cars,觀察車速(speed)與煞車距離(dist)之關係。
# aes:輸入x軸與y軸資料
ggplot(cars, aes(x = speed, y = dist))發現點不會自動顯示出來,使用“+”來進行函數疊加。
# geom_point():畫點
ggplot(cars, aes(x = speed, y = dist)) +
geom_point()亦可以先將畫布指派到變數
ggplot_without_points <- ggplot(cars, aes(x = speed, y = dist))
ggplot_without_points +
geom_point()加入圖片標題(類似main)與座標名稱
# ggtitle:圖片標題
# xlab():給x座標名稱
# ylab():給y座標名稱
ggplot(cars, aes(x = speed, y = dist)) +
geom_point() +
ggtitle("Car speed vs. braking distance") +
xlab("Speed") +
ylab("Dist")# geom_smooth(method = …):加入最適線,“lm”代表回歸方法用linear modol
ggplot(cars, aes(x = speed, y = dist)) +
geom_point() +
ggtitle("Car speed vs. braking distance") +
xlab("Speed") +
ylab("Dist") +
geom_smooth(method = "lm")可至?geom_smooth查詢其他回歸方法
# se:是否顯示confidence interval,預設為TRUE
ggplot(cars, aes(x = speed, y = dist)) +
geom_point() +
ggtitle("Car speed vs. braking distance") +
xlab("Speed") +
ylab("Dist") +
geom_smooth(method = "lm", se = FALSE)玩具資料採用iris,依照不同品種(Species)來觀察萼片(Sepal)之長度(Length)與寬度(Width)之分布。
aes()中加入引數color
尚未依照品種區分之散佈圖
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point() +
ggtitle("Sepal.Length vs. Sepal.Width") +
xlab("Sepal length") +
ylab("Sepal width")依品種(Species)按顏色區分
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
ggtitle("Sepal.Length vs. Sepal.Width") +
xlab("Sepal length") +
ylab("Sepal width")玩具資料採用diamonds,觀察鑽石長度(x)與闊度(y)之關係。
my_diamonds <- head(diamonds, n = 15L)# geom_line():畫線
ggplot(my_diamonds, aes(x = x, y = y)) +
geom_line()發現geom_line的畫線順序自動會以x軸資料之大小排序
ggplot(my_diamonds, aes(x = x, y = y)) +
geom_point() +
geom_line()# colour:調整顏色
ggplot(my_diamonds, aes(x = x, y = y)) +
geom_point(colour = "green") +
geom_line(colour = "red")線圖常用來視覺化時間序列
head(economics)## # A tibble: 6 × 6
## date pce pop psavert uempmed unemploy
## <date> <dbl> <int> <dbl> <dbl> <int>
## 1 1967-07-01 507.4 198712 12.5 4.5 2944
## 2 1967-08-01 510.5 198911 12.5 4.7 2945
## 3 1967-09-01 516.3 199113 11.7 4.6 2958
## 4 1967-10-01 512.9 199311 12.5 4.9 3143
## 5 1967-11-01 518.1 199498 12.5 4.7 3066
## 6 1967-12-01 525.8 199657 12.1 4.8 3018
ggplot(economics, aes(x = date, y = unemploy)) +
geom_line()玩具資料採用iris,觀察萼片(Sepal)之長度(Length)之分布。
使用ggplot() + geom_histogram()繪製直方圖
# binwidth:調整分箱寬度
ggplot(iris, aes(x = Sepal.Length)) +
geom_histogram(binwidth = 0.2)# facet_wrap():納入單一類別變數分層畫圖
ggplot(iris, aes(x = Sepal.Length)) +
geom_histogram(binwidth = 0.2) +
facet_wrap(~ Species)加入ncol引數,可調整畫布規格。
ggplot(iris, aes(x = Sepal.Length)) +
geom_histogram(binwidth = 0.2) +
facet_wrap(~ Species, ncol = 1)# facet_grid():納入兩個類別變數分層畫圖
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = color)) +
facet_grid(cut ~ clarity)機率密度圖可想像成將寬度縮成很細的直方圖。
# fill:所要填滿之顏色
ggplot(diamonds) +
geom_density(aes(x = carat), fill = "grey50")玩具資料採用iris,分別觀察不同品種(Species)的萼片(Sepal)之長度(Length)之分布。
使用ggplot() + geom_boxplot()繪製盒鬚圖
# 預設要讓aes吃一個x值
ggplot(iris, aes(x = 1, y = Sepal.Length)) +
geom_boxplot()# aes() 指定 x = 納入類別變數的展開
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
geom_boxplot()小提琴圖可想像成箱子按曲線呈現的箱型圖。
ggplot(diamonds, aes(x = 1, y = carat)) +
geom_violin()按cut分類
ggplot(diamonds, aes(x = cut, y = carat)) +
geom_violin()玩具資料採用mtcars,為不同齒輪(gear)之車輛數作計數,以及列出每種車型的馬力(hp)。
使用ggplot() + geom_bar()繪製長條圖
ggplot(mtcars, aes(x = gear)) +
geom_bar()ggplot(mtcars, aes(x = row.names(mtcars), y = hp)) +
geom_bar()
發現上述指令會出現error訊息,原因是預設y值為"count",作計數的動作。然而在此想呈現馬力(hp)數值高低,因此得調整函數geom_bar中的引數stat。
# geom_bar(stat = “identity”):呈現數值
ggplot(mtcars, aes(x = row.names(mtcars), y = hp)) +
geom_bar(stat = "identity")# cood_flip():座標翻轉
ggplot(mtcars, aes(x = row.names(mtcars), y = hp)) +
geom_bar(stat = "identity") + coord_flip()玩具資料採用mpg,觀察各車型(class)的數量,並區分車輪驅動方式(drv)。
# geom_bar(aes(fill = (drv))):依照類別drv為長條圖著色
ggplot(mpg, aes(x = class)) +
geom_bar(aes(fill = drv))可選擇依類別將長條分開
# 加入 position = “dodge” 參數
ggplot(mpg, aes(x = class)) +
geom_bar(aes(fill = drv), position = "dodge")亦可觀察各車型(class),各車輪驅動方式(drv)之百分比。
# 加入 position = “fill” 參數
ggplot(mpg, aes(x = class)) +
geom_bar(aes(fill = drv), position = "fill") +
ylab("Percentage")ggplotly)plotly# 安裝套件
install.packages("plotly")
# 載入套件
library(plotly)# ggplotly():新增互動功能
static_gg <- ggplot(mpg, aes(x = class)) +
geom_bar(aes(fill = drv))
ggplotly(static_gg)# 安裝套件lubridate
install.packages("lubridate")
# 載入套件lubridate和scales
require(lubridate)
require(scales)僅採用economics資料中2000年後的資料
# label:TRUE代表月份按名稱顯示而非數字
economics$year <- year(economics$date)
economics$month <- month(economics$date, label = TRUE)
econ2000 <- economics[which(economics$year >= 2000), ]
# econ2000 <- economics[economics$year >= 2000, ] 亦可依不同顏色標記不同年份,並按年份將劃分不同的線
ggplot(econ2000, aes(x = month, y = pop)) +
geom_line(aes(color = factor(year), group = year))# 修改說明文字
# 用comma修改y軸格式
# 新增標題和x軸, y軸的標籤
ggplot(econ2000, aes(x = month, y = pop)) +
geom_line(aes(color = factor(year), group = year)) +
scale_color_discrete(name = "Year") +
scale_y_continuous(labels = comma) +
labs(title = "Population Growth", x = "Month", y = "Population")ggthemes# 安裝套件
install.packages("ggthemes")
# 載入套件
require(ggthemes)# 原本樣式
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = color))# The Economist
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = color)) +
theme_economist() +
scale_colour_economist()# Excel
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = color)) +
theme_excel() +
scale_colour_excel()# Edward Tufte
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = color)) +
theme_tufte()# The Wall Street Journal
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = color)) +
theme_wsj()